import axios from 'axios'
import qs from 'qs'
import router from '../router'

let instance = axios.create(({
  timeout: 3000,       
  withCredentials: true, 
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded' // 设置post请求头
  },
  transformRequest: data => qs.stringify(data)  }))

instance.interceptors.request.use(config => {
  const token = localStorage.getItem('usertoken')
  token && (config.headers.Authorization = token)
  return config
}, error => {
  router.push({
    path: '/login',
    query: { redirect: router.currentRoute.fullPath }
  })
  return Promise.reject(error);
})

instance.interceptors.response.use(res => {
  let token = res.headers.authorization
  if (token) {
    localStorage.setItem('userToken', token)
  }
  return res.data
}, error => {
  if (error && error.response) {
    switch (error.response.status) {
      case 401:
        console.log("尚未登录，请登录");
        this.$route.replace({
          path: '/login',
          query: { redirect: router.currentRoute.fullPath }
        });
        break;
      case 403:
        console.log("登录过期，请重新登录");
        localStorage.removeItem('token');
          setTimeout(() => {
          router.replace({
            path: '/login',
            query: {
              redirect: router.currentRoute.fullPath
            }
          });
        }, 1000);
        break;
      case 404:
        console.log("404");
        break;
      case 500:
        console.log("服务器出错");
        break;
      default:
        console.log("未知错误");
    }
  }
  return Promise.reject(error);
})
const api = {
  get (url, params) {
    return instance({
      method: 'get',
      url,
      params,
    })
  },
  /**
   * post 请求，传递 json数据
   * @param url接口地址
   * @param data
   */
  post (url, data) {
    return instance({
      method: 'post',
      url,
      data: data,
    })
  },
}

export default api